package com.bos.prop.controller.json;

import com.bos.dao.TahunDAO;
import com.bos.domain.Tahun;
import com.bos.editor.TahunEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class TahunJsonController {

    private TahunDAO tahunDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setTahunDAO(TahunDAO tahunDAO) {
        this.tahunDAO = tahunDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/tahun.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Tahun> index() {
        try {
            return tahunDAO.getAll();
        } catch (DataAccessException e) {
            return new ArrayList<Tahun>();
        }
    }

    @RequestMapping(value = "/tahun/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Tahun show(@PathVariable("id") Tahun tahun) {
        return tahun;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Tahun.class, new TahunEditor(tahunDAO));
    }

}
